package com.lions.client.view.basepanel;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.lions.client.controller.Controller;
import com.lions.client.model.Model;
import com.lions.client.view.audio.AudioOutput;
import com.lions.client.view.mappanel.MapPanel;
import com.lions.client.view.popup.MyPopupPanel;

// TODO: Auto-generated Javadoc
/**
 * The Class ConfigurationsPanel.
 */
public class ConfigurationsPanel extends VerticalPanel implements Comparable<ConfigurationsPanel>{
	
	/**
	 * Instantiates a new configurations panel.
	 *
	 * @param m the m
	 * @param c the c
	 * @param p the p
	 * @param basePicLoc the base pic loc
	 * @param plain1X the plain1 x
	 * @param plain1Y the plain1 y
	 * @param plain2X the plain2 x
	 * @param plain2Y the plain2 y
	 * @param plain3X the plain3 x
	 * @param plain3Y the plain3 y
	 * @param airlockX the airlock x
	 * @param airlockY the airlock y
	 * @param controlX the control x
	 * @param controlY the control y
	 * @param powerX the power x
	 * @param powerY the power y
	 * @param foodX the food x
	 * @param foodY the food y
	 * @param dormX the dorm x
	 * @param dormY the dorm y
	 * @param canteenX the canteen x
	 * @param canteenY the canteen y
	 * @param sanX the san x
	 * @param sanY the san y
	 */
	public ConfigurationsPanel(Model m, Controller c, MapPanel p, String basePicLoc,
			final int plain1X, final int plain1Y, 
			final int plain2X, final int plain2Y, 
			final int plain3X, final int plain3Y, 
			final int airlockX, final int airlockY, 
			final int controlX, final int controlY, 
			final int powerX, final int powerY, 
			final int foodX, final int foodY, 
			final int dormX, final int dormY, 
			final int canteenX, final int canteenY, 
			final int sanX, final int sanY){

				routeIndex = 0;
		//set the location for the base image
				basePicFile = basePicLoc;

				model = m;
				controller = c;
				mapPanel = p;
				
		//pass base dimensions through to MinBaseBuilder
		constructor = new MinBaseBuilder(model, mapPanel, 
				plain1X, plain1Y, 
				plain2X, plain2Y, 
				plain3X, plain3Y, 
				airlockX, airlockY, 
				controlX, controlY, 
				powerX, powerY, 
				foodX, foodY, 
				dormX, dormY, 
				canteenX, canteenY, 
				sanX, sanY);
		
		
		movementPoints = constructor.getMovementPoints();
		//the commit button: Will commit base changes to master list
		commit = new Button("COMMIT", new ClickHandler(){
			public void onClick(ClickEvent event) {
				constructor.commit();
				mapPanel.populate();
				toggleButtons(false);
			}
		});

		//the revert button: removes the base display and returns view to master list view
		revert = new Button("REVERT", new ClickHandler(){
			public void onClick(ClickEvent event) {
				mapPanel.populate();
				toggleButtons(false);
			}
		});
		//the horizontal panel that holds commit and revert buttons
		bottom = initStandardButtons(commit, revert);
		preview = new Button("Preview", new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(model.checkMin()){
					
					constructor.display();
					toggleButtons(true);
					
				} else { 
					new MyPopupPanel("Base Configuration",
							"Minimum Base Configuration Not Met\n");
					new AudioOutput().playBaseError();
				}
			}
		});
		//controller.getSpeech().addVoiceHandler(build, "build base");
		preview.setPixelSize(100, 100);
		preview.setStyleName("buttonstyle");
		top = new HorizontalPanel();
		top.add(new Image(basePicFile));
		top.add(preview);
		route = new Button("Route", new ClickHandler(){
			public void onClick(ClickEvent event) {
				constructor.route(routeIndex);
				setRouteText(routeIndex);
			}
		});
		route.setPixelSize(130, 100);
		route.setStyleName("buttonstyle");
		
		next = new Button(">", new ClickHandler(){
			public void onClick(ClickEvent event) {
				routeIndex++;
				if(routeIndex >= constructor.getRoutingListSize()){
					routeIndex = 0;
				}
				setRouteText(routeIndex);
				constructor.route(routeIndex);
			}
		});
		next.setStyleName("buttonstyle");
		next.setPixelSize(35, 100);
		prev = new Button("<", new ClickHandler(){
			public void onClick(ClickEvent event) {
				routeIndex--;
				if(routeIndex < 0){
					routeIndex = constructor.getRoutingListSize() - 1;
				}
				constructor.route(routeIndex);
				setRouteText(routeIndex);
			}
		});
		prev.setStyleName("buttonstyle");
		prev.setPixelSize(35, 100);
		
		indexer = new HorizontalPanel();
		indexer.setPixelSize(200, 100);
		indexer.add(prev);
		indexer.add(route);
		indexer.add(next);
		
		routeLabel = new Label("Routing Information");
		routeLabel.setPixelSize(200, 50);
		routeLabel.setStyleName("popup");
		
		setXY = new Button("Set New Center Of Gravity", new ClickHandler(){
			public void onClick(ClickEvent event) {
				new CenterPopup(constructor);
			}
		});
		setXY.setPixelSize(200, 50);
		setXY.setStyleName("buttonstyle");
		
		toggleButtons(false);
		add(top);
		add(indexer);
		add(bottom);
		add(routeLabel);
		add(setXY);
		
	}

	/**
	 * Inits the standard buttons.
	 *
	 * @param c the c
	 * @param r the r
	 * @return the horizontal panel
	 */
	public HorizontalPanel initStandardButtons(Button c, Button r){
		HorizontalPanel tempPanel = new HorizontalPanel();
		tempPanel.setPixelSize(200, 100);
		c.setPixelSize(100, 100);
		r.setPixelSize(100, 100);
		c.setStyleName("buttonstyle");
		r.setStyleName("buttonstyle");
		tempPanel.add(c);
		tempPanel.add(r);
		return tempPanel;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(ConfigurationsPanel o) {
		return movementPoints.compareTo(o.getMovementPoints());
	}
	
	/**
	 * Gets the movement points.
	 *
	 * @return the movement points
	 */
	public Integer getMovementPoints(){
		return movementPoints;
	}
	
	/**
	 * Toggle buttons.
	 *
	 * @param nextValue the next value
	 */
	public void toggleButtons(boolean nextValue){
		revert.setVisible(nextValue);
		commit.setVisible(nextValue);
		route.setVisible(nextValue);
		next.setVisible(nextValue);
		prev.setVisible(nextValue);
		revert.setEnabled(nextValue);
		commit.setEnabled(nextValue);
		route.setEnabled(nextValue);
		next.setEnabled(nextValue);
		prev.setEnabled(nextValue);
	}
	
	/**
	 * Sets the route text.
	 *
	 * @param index the new route text
	 */
	public void setRouteText(int index){
		routeLabel.setText("Routing" + constructor.getRouteModule(index).getType() 
		+ "Module at (" + constructor.getRouteModule(index).getX() 
		+ ", " + constructor.getRouteModule(index).getY() 
		+ ") to destination.");
	}
	
	/** The preview. */
	private Button preview;
	
	/** The revert. */
	private Button revert;
	
	/** The commit. */
	private Button commit;
	
	/** The route. */
	private Button route;
	
	/** The next. */
	private Button next;
	
	/** The prev. */
	private Button prev;
	
	/** The top. */
	private HorizontalPanel top;
	
	/** The bottom. */
	private HorizontalPanel bottom;
	
	/** The indexer. */
	private HorizontalPanel indexer;
	
	/** The map panel. */
	private MapPanel mapPanel;
	
	/** The controller. */
	private Controller controller;
	
	/** The model. */
	private Model model;
	
	/** The base pic file. */
	private String basePicFile;
	
	/** The airlock y. */
	int plain1X, plain1Y, plain2X, plain2Y, plain3X, plain3Y, airlockX, airlockY; 
	
	/** The dorm y. */
	int controlX, controlY, powerX, powerY, foodX, foodY, dormX, dormY; 
	
	/** The san y. */
	int canteenX, canteenY, sanX, sanY;
	
	/** The movement points. */
	private Integer movementPoints;
	
	/** The constructor. */
	private MinBaseBuilder constructor;
	
	/** The route index. */
	private int routeIndex;
	
	/** The route label. */
	private Label routeLabel;
	
	/**
	 * The setXY button
	 */
	private Button setXY;
	
}
